﻿using System;
using System.Text;
using System.Web;
using QuanLyHopDong.Biz;
using QuanLyHopDong.Biz.Model;

namespace QuanLyHopDong.Web.HopDong
{
    public partial class XuatBang4A : System.Web.UI.Page
    {
        private int _hopDongId;

        private int HopDongId
        {
            get
            {
                if (_hopDongId <= 0)
                    int.TryParse(Request.QueryString["hopDongId"], out _hopDongId);
                return _hopDongId;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            var hopDong = new HopDongBiz().GetById(HopDongId);

            var html = GenerateHtml(hopDong);

            Response.Clear();
            EnableViewState = false;
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=" +
                string.Format("Bang_4A_Hop_Dong_{1}_{0:dd-MM-yyyy_hhmmss}.xls", DateTime.Now, HttpUtility.UrlEncode(hopDong.SoHopDong)));
            Response.ContentEncoding = Encoding.UTF8;
            Response.ContentType = "application/vnd.ms-excel";
            Response.BinaryWrite(Encoding.Unicode.GetPreamble());
            var bytes = Encoding.Unicode.GetBytes(html);
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }

        private string GenerateHtml(HopDongView hopDong)
        {
            var vatTuViews = new KhoiLuongVatTuBiz().XuatBang4A(HopDongId);

            var sbHtml = new StringBuilder();
            sbHtml.Append(
                "<html><body style='font-size:12px !important;font-family:Times New Roman !important;'><head><meta charset=\"utf-8\" />");
            sbHtml.Append("<style type='text/css'>td {vertical-align:top;}</style>");
            sbHtml.Append("</head><body>");

            sbHtml.Append("<table cellpadding='0' cellspacing='0' width='100%' style='border-collapse:collapse;'>");

            // header columns
            sbHtml.Append("<tr>");
            sbHtml.AppendFormat("<th rowspan='2' style='border:solid 0.1pt; text-align:center;'>{0}</th>", "Hợp đồng");
            sbHtml.AppendFormat("<th rowspan='2' style='border:solid 0.1pt; text-align:center;'>{0}</th>", "Ngày tháng");
            sbHtml.AppendFormat("<th rowspan='2' style='border:solid 0.1pt; text-align:center;'>{0}</th>", "Số PXK");
            sbHtml.AppendFormat("<th rowspan='2' style='border:solid 0.1pt; text-align:center;'>{0}</th>", "Ngày CT");
            sbHtml.AppendFormat("<th rowspan='2' style='border:solid 0.1pt; text-align:center;'>{0}</th>", "Tên vật tư");
            sbHtml.AppendFormat("<th rowspan='2' style='border:solid 0.1pt; text-align:center;'>{0}</th>", "Mã công trình");
            sbHtml.AppendFormat("<th colspan='2' style='border:solid 0.1pt; text-align:center;'>{0}</th>", "Xuất kho");
            sbHtml.AppendFormat("<th colspan='2' style='border:solid 0.1pt; text-align:center;'>{0}</th>", "Số thực tế thi công");
            sbHtml.AppendFormat("<th colspan='2' style='border:solid 0.1pt; text-align:center;'>{0}</th>", "Số đã thu hồi");
            sbHtml.Append("</tr>");
            sbHtml.Append("<tr>");
            sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:center;'>{0}</td>", "SL");
            sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:center;'>{0}</td>", "Thành tiền");
            sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:center;'>{0}</td>", "SL");
            sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:center;'>{0}</td>", "Thành tiền");
            sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:center;'>{0}</td>", "SL");
            sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:center;'>{0}</td>", "Thành tiền");
            sbHtml.Append("</tr>");

            // rows
            var i = 0;
            foreach (var vatTuView in vatTuViews)
            {
                sbHtml.Append("<tr>");
                if (i == 0)
                {
                    sbHtml.AppendFormat("<td rowspan='{1}' style='border:solid 0.1pt;'>{0}</td>", hopDong.SoHopDong,
                        vatTuViews.Count);
                    sbHtml.AppendFormat("<td rowspan='{1}' style='border:solid 0.1pt; text-align:right;'><span>{0:dd/MM/yyyy}</span></td>",
                        hopDong.NgayQuyetToan, vatTuViews.Count);
                }
                sbHtml.AppendFormat("<td style='border:solid 0.1pt;'>{0}</td>", vatTuView.SoPhieu);
                sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:right;'>{0:dd/MM/yyyy}</td>", vatTuView.NgayChungTu);
                sbHtml.AppendFormat("<td style='border:solid 0.1pt;'>{0}</td>", vatTuView.Name);
                sbHtml.AppendFormat("<td style='border:solid 0.1pt;'>{0}</td>", vatTuView.MaTuyen);
                sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:right;'>{0}</td>", vatTuView.ThiCong == 0 ? null : vatTuView.ThiCong); // Xuat kho
                sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:right;'>{0}</td>", string.Empty);
                sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:right;'>{0}</td>", vatTuView.ThucTe == 0 ? null : vatTuView.ThucTe); // thuc te
                sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:right;'>{0}</td>", string.Empty);
                sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:right;'>{0}</td>", vatTuView.ThuHoi == 0 ? null : vatTuView.ThuHoi); // con lai
                sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:right;'>{0}</td>", string.Empty);
                sbHtml.Append("</tr>");

                i++;
            }

            sbHtml.Append("</body></html>");

            return sbHtml.ToString();
        }
    }
}